import { PROCESS_NODE_STATUS } from '@/utils/const/process';
export default {
  methods: {
    // 审批日志和评论按时间排序
    setProcessList(processList) {
      let endNode = []; // 日志流需展示的首个审批节点
      let processFirstList = []; // 接口返回的首个非转交类型节点
      if (processList[0] && processList[0].items && processList[0].items[0]) {
        // 重新提交节点单独处理
        const { status } = processList[0].items[0];
        const isPersonChangeNode = status === PROCESS_NODE_STATUS.PERSON_CHANGE || status === PROCESS_NODE_STATUS.FORWARDED;
        if (isPersonChangeNode) {
          for (let i = 1; i < processList.length; i++) {
            if (processList[i] && processList[i].items && processList[i].items[0] && processList[i].items[0].status !== PROCESS_NODE_STATUS.PERSON_CHANGE) {
              processFirstList = processList.splice(i, 1);
              break;
            }
          }
        } else {
          processFirstList = processList.splice(0, 1);
        }
        if (processFirstList[0].items.length > 1) {
          // 处理日志流首个节点，需要拆分
          const { approvalNodes, approvedNodes } = this.setEndNode(processFirstList);
          endNode = approvalNodes;
          processList.unshift(...approvedNodes);
        } else {
          endNode = processFirstList;
        }
      }
      const comments = this.formatCommentList();
      const collectionList = processList.concat(comments);
      const processCollectionList = collectionList.sort((a, b) => {
        const aTime = a.items.every(subItem => subItem.time) ? a.items[0].time : a.nodeStatus === 1 ? 9999999999999 : a.items[0].time;
        const bTime = b.items.every(subItem => subItem.time) ? b.items[0].time : b.nodeStatus === 1 ? 9999999999999 : b.items[0].time;
        return bTime - aTime;
      });
      endNode.length && processCollectionList.unshift(endNode[0]);
      return this.splitProcessList(processCollectionList);
    },
    /**
     * 1、拆分单个审批节点含有多个审批的情况
     *    - 拆分显示所有非审批中的且/或审批
     * 2、过滤未审批节点
     * */
    splitProcessList(list) {
      const result = [];
      list.forEach(item => {
        if (item.items.length > 1 && item.nodeType !== 3 && !item.items.every(i => i.status === PROCESS_NODE_STATUS.PENDING)) {
          const approvalProcess = item.items.filter(i => i.status === PROCESS_NODE_STATUS.PENDING); // 审批中
          if (approvalProcess.length) {
            approvalProcess.forEach(approvalItem => {
              approvalItem.time = null;
            });
            const approvalBaseItem = _.cloneDeep(item);
            approvalBaseItem.items = approvalProcess;
            result.push(approvalBaseItem);
          }

          const approvedProcess = item.items.filter(i => ![PROCESS_NODE_STATUS.PENDING, PROCESS_NODE_STATUS.NOT_PERFORMED].includes(i.status)); // 已审批
          approvedProcess.forEach(approvedItem => {
            const approvedBaseItem = _.cloneDeep(item);
            approvedBaseItem.items = [approvedItem];
            approvedBaseItem.nodeStatus = 0;
            result.push(approvedBaseItem);
          });
        } else if (item.items.length === 1) { // 过滤未审批节点
          if (item.items[0].status !== PROCESS_NODE_STATUS.NOT_PERFORMED || item.items[0].title === '任务结束') {
            result.push(item);
          }
        } else {
          result.push(item);
        }
      });
      return result;
    },
    // 若首个节点含审批中状态，获取审批中子项，否则取第一个子审批项
    setEndNode(list) {
      let approvedNodes = [];
      const splitList = this.splitProcessList(list);
      let approvalNodes = splitList.filter(item => item.items[0].status === PROCESS_NODE_STATUS.PENDING);
      if (approvalNodes.length) {
        approvedNodes = splitList.filter(item => item.items[0].status !== PROCESS_NODE_STATUS.PENDING);
      } else {
        approvalNodes = splitList[0];
        approvedNodes = splitList.splice(1, splitList.length - 1);
      }
      return { approvalNodes, approvedNodes };
    },
    formatCommentList() {
      const result = [];
      this.commentList.forEach(item => {
        item.time = item.sendTime * 1000;
        const obj = Object.create(null);
        obj.nodeStatus = -1;
        obj.items = [item];
        result.push(obj);
      });
      return result;
    }
  }
};
